{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/144_chart_features.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Feature and FeatureCollection Charts**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import calendar\n",
    "import ee\n",
    "import geemap\n",
    "from geemap import chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## feature_by_feature\n",
    "\n",
    "Features are plotted along the x-axis, labeled by values of a selected property. Series are represented by adjacent columns defined by a list of property names whose values are plotted along the y-axis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "features = ecoregions.select(\"[0-9][0-9]_tmean|label\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_df(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_property = \"label\"\n",
    "y_properties = [str(x).zfill(2) + \"_tmean\" for x in range(1, 13)]\n",
    "\n",
    "labels = calendar.month_abbr[1:]  # a list of month labels, e.g. ['Jan', 'Feb', ...]\n",
    "\n",
    "colors = [\n",
    "    \"#604791\",\n",
    "    \"#1d6b99\",\n",
    "    \"#39a8a7\",\n",
    "    \"#0f8755\",\n",
    "    \"#76b349\",\n",
    "    \"#f0af07\",\n",
    "    \"#e37d05\",\n",
    "    \"#cf513e\",\n",
    "    \"#96356f\",\n",
    "    \"#724173\",\n",
    "    \"#9c4f97\",\n",
    "    \"#696969\",\n",
    "]\n",
    "title = \"Average Monthly Temperature by Ecoregion\"\n",
    "x_label = \"Ecoregion\"\n",
    "y_label = \"Temperature\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.feature_by_feature(\n",
    "    features,\n",
    "    x_property,\n",
    "    y_properties,\n",
    "    colors=colors,\n",
    "    labels=labels,\n",
    "    title=title,\n",
    "    x_label=x_label,\n",
    "    y_label=y_label,\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/MZa99Vf.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## feature.by_property"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "features = ecoregions.select(\"[0-9][0-9]_ppt|label\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_df(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "keys = [str(x).zfill(2) + \"_ppt\" for x in range(1, 13)]\n",
    "values = calendar.month_abbr[1:]  # a list of month labels, e.g. ['Jan', 'Feb', ...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_properties = dict(zip(keys, values))\n",
    "series_property = \"label\"\n",
    "title = \"Average Ecoregion Precipitation by Month\"\n",
    "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.feature_by_property(\n",
    "    features,\n",
    "    x_properties,\n",
    "    series_property,\n",
    "    title=title,\n",
    "    colors=colors,\n",
    "    x_label=\"Month\",\n",
    "    y_label=\"Precipitation (mm)\",\n",
    "    legend_location=\"top-left\",\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/6RhuUc7.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## feature_groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "features = ecoregions.select(\"[0-9][0-9]_ppt|label\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "features = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "x_property = \"label\"\n",
    "y_property = \"01_tmean\"\n",
    "series_property = \"warm\"\n",
    "title = \"Average January Temperature by Ecoregion\"\n",
    "colors = [\"#cf513e\", \"#1d6b99\"]\n",
    "labels = [\"Warm\", \"Cold\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chart.feature_groups(\n",
    "    features,\n",
    "    x_property,\n",
    "    y_property,\n",
    "    series_property,\n",
    "    title=title,\n",
    "    colors=colors,\n",
    "    x_label=\"Ecoregion\",\n",
    "    y_label=\"January Temperature (°C)\",\n",
    "    legend_location=\"top-right\",\n",
    "    labels=labels,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/YFZlJtc.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## feature_histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ee.ImageCollection(\"OREGONSTATE/PRISM/Norm91m\").toBands()\n",
    "region = ee.Geometry.Rectangle(-123.41, 40.43, -116.38, 45.14)\n",
    "features = source.sample(region, 5000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_df(features.limit(5).select([\"07_ppt\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "property = \"07_ppt\"\n",
    "title = \"July Precipitation Distribution for NW USA\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.feature_histogram(\n",
    "    features,\n",
    "    property,\n",
    "    max_buckets=None,\n",
    "    title=title,\n",
    "    x_label=\"Precipitation (mm)\",\n",
    "    y_label=\"Pixel Count\",\n",
    "    colors=[\"#1d6b99\"],\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/ErIp7Oy.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
